<template>
  <div :class = "formDatas.isEdit?'field-item design-star-horse':''">
    <el-form-item
        :label = "formItem.preps['label']"
        :prop = "formItem.preps['name']"
        :required = "formItem.preps['required']"
        @click = "selectData(formItem.preps)">
      <slot></slot>
    </el-form-item>
    <div
        class = "field-action"
        v-if = "formDatas.activeId==formItem.preps.id &&formDatas.isEdit">
      <i @click.stop = "selectParentContainer()" title = "选择父容器">
        <user-icon icon = "fa-solid fa-hand-pointer" style = "color:#e3e9f2;"/>
      </i>
      <i @click.stop = "moveUpItem(formItem.preps)" title = "上移">
        <user-icon icon = "fa-solid fa-arrow-up" style = "color:#e3e9f2;"/>
      </i>
      <i @click.stop = "moveDownItem(formItem.preps)" title = "下移">
        <user-icon icon = "fa-solid fa-arrow-down" style = "color:#e3e9f2;"/>
      </i>
      <i @click.stop = "removeItem(formItem.preps)" title = "删除">
        <user-icon icon = "trash" style = "color:#e3e9f2;"/>
      </i>
    </div>
    <div class = "drag-handler background-opacity" v-if = "formDatas.isEdit">
      <i title = "拖动">
        <user-icon icon = "fas fa-expand-arrows-alt" style = "color:#e3e9f2;"/>
      </i>
      <i>
        <user-icon icon = "hide" style = "color:#e3e9f2;"/>
      </i>
    </div>
  </div>
</template>

<script>
import {inject} from 'vue'

export default {
  name: 'form-item',
  props: {
    parentField: {type: Object},
    parentId: {type: Object},
    formItem: {
      type: Object,
      required: true
    },
    formDatas: {type: Object, required: true}
  },
  setup() {
    let activeItemFun = inject("activeItemFun");
    return {activeItemFun}
  },
  data() {
    return {
      parentCompType:
          this.parentField && (this.parentField.type == 'box' || this.parentField.type == 'table')
              ? 'container' : 'item'
    }
  },
  methods: {
    selectParentContainer() {
      let _this = this
      _this.activeItemFun("selectItem", _this.parentField, "item");
    },
    selectData(data) {
      let _this = this
      _this.activeItemFun("selectItem", data, _this.parentCompType);
    },
    moveUpItem(formItem) {
      let dataList = this.formDatas.dataList
      let compType = this.parentCompType
      if (compType === 'item') {
        for (let i = 0; i < dataList.length; i++) {
          if (formItem.id === dataList[i].id && i > 0) {
            let temp = dataList[i]
            dataList[i] = dataList[i - 1]
            dataList[i - 1] = temp
            break
          }
        }
      } else {
        for (let i = 0; i < dataList.length; i++) {
          let dataTemp = dataList[i]
          if (dataTemp.compType !== 'container') {
            continue
          }
          let elements = dataTemp.preps.elements
          for (let index in elements) {
            let sdataTemp = elements[index]
            if (sdataTemp.columns.length > 0) {
              for (let i = 0; i < sdataTemp.columns.length; i++) {
                let items = sdataTemp.columns[i].items
                for (let j = 0; j < items.length; j++) {
                  if (formItem.id === items[j].id && j > 0) {
                    let temp = items[j]
                    items[j] = items[j - 1]
                    items[j - 1] = temp
                    break
                  }
                }
              }
            }
          }
        }
      }
    },
    moveDownItem(formItem) {
      let dataList = this.formDatas.dataList
      let compType = this.parentCompType
      if (compType === 'item') {
        for (let i = 0; i < dataList.length; i++) {
          if (formItem.id === dataList[i].id && i < dataList.length - 1) {
            let temp = dataList[i]
            dataList[i] = dataList[i + 1]
            dataList[i + 1] = temp
            break
          }
        }
      } else {
        for (let i = 0; i < dataList.length; i++) {
          let dataTemp = dataList[i]
          if (dataTemp.compType !== 'container') {
            continue
          }
          let elements = dataTemp.preps.elements
          for (let index in elements) {
            let sdataTemp = elements[index]
            if (sdataTemp.columns.length > 0) {
              for (let i = 0; i < sdataTemp.columns.length; i++) {
                let items = sdataTemp.columns[i].items
                for (let j = 0; j < items.length; j++) {
                  if (formItem.id === items[j].id && j < items.length - 1) {
                    let temp = items[j]
                    items[j] = items[j + 1]
                    items[j + 1] = temp
                    break
                  }
                }
              }
            }
          }
        }
      }
    },
    removeItem(formItem) {
      let dataList = this.formDatas.dataList
      let compType = this.parentCompType
      if (compType === 'item') {
        for (let i = 0; i < dataList.length; i++) {
          if (formItem.id === dataList[i].id) {
            dataList.splice(i, 1)
            break
          }
        }
      } else {
        for (let i = 0; i < dataList.length; i++) {
          let dataTemp = dataList[i]
          if (dataTemp.compType !== 'container') {
            continue
          }
          let elements = dataTemp.preps.elements
          for (let index in elements) {
            let sdataTemp = elements[index]
            if (sdataTemp.columns.length > 0) {
              for (let i = 0; i < sdataTemp.columns.length; i++) {
                let items = sdataTemp.columns[i].items
                for (let j = 0; j < items.length; j++) {
                  if (formItem.id === items[j].id) {
                    items.splice(j, 1)
                    break
                  }
                }
              }
            }
          }
        }
      }
    }
  },
  watch: {
    'formItem.activeId': {
      deep: true,
      handler: function (v) {
        console.log(v)
      }
    }
  }
}
</script>

<style lang = "scss" scoped>
.design-star-horse {
  margin-bottom: 5px;
  border: 2px dotted greenyellow;
  padding: 3px;
  z-index: 0;
}

.field-item {
  position: relative;

  .field-action {
    position: absolute;
    //bottom: -24px;
    bottom: 0;
    right: -2px;
    height: 22px;
    line-height: 22px;
    background: #3a8ee6;
    z-index: 9999999;

    i {
      font-size: 14px;
      color: #fff;
      margin: 0 3px;
      cursor: pointer;
    }
  }

  .drag-handler {
    position: absolute;
    top: 0;
    left: -1px;
    height: 20px;
    line-height: 20px;
    background: #3a8ee6;
    z-index: 9;
    opacity: 0;

    i {
      font-size: 12px;
      font-style: normal;
      color: #409eff;
      margin: 4px;
      cursor: move;
    }

    &:hover {
      opacity: 1;
      background: #3a8ee6;
    }
  }
}

.field-action,
.drag-handler {
  :deep(.svg-icon) {
    margin-left: 0;
    margin-right: 0;
  }
}

.el-form-item {
  //margin-bottom: 0 !important;
  //margin-bottom: 6px;

  //margin-top: 2px;
  position: relative;

  :deep(.el-form-item__label) {
    white-space: nowrap;
    text-overflow: ellipsis;
  }

  :deep(.el-form-item__content) {
    //position: unset;  /* TODO: 忘了这个样式设置是为了解决什么问题？？ */
  }

  span.custom-label i {
    margin: 0 3px;
  }

  /* 隐藏Chrome浏览器中el-input数字输入框右侧的上下调整小箭头 */
  :deep(.hide-spin-button) input::-webkit-outer-spin-button,
  :deep(.hide-spin-button) input::-webkit-inner-spin-button {
    -webkit-appearance: none !important;
  }

  /* 隐藏Firefox浏览器中el-input数字输入框右侧的上下调整小箭头 */
  :deep(.hide-spin-button) input[type='number'] {
    -moz-appearance: textfield;
  }
}

.required :deep(.el-form-item__label)::before {
  content: '*';
  color: #f56c6c;
  margin-right: 4px;
}

.static-content-item {
  min-height: 20px;
  display: flex; /* 垂直居中 */
  align-items: center; /* 垂直居中 */

  :deep(.el-divider--horizontal) {
    margin: 0;
  }
}

.el-form-item.selected,
.static-content-item.selected {
  outline: 2px solid #1989fa;
}

:deep(.label-left-align) .el-form-item__label {
  text-align: left;
  justify-content: flex-start !important;
}

:deep(.label-center-align) .el-form-item__label {
  text-align: center;
  justify-content: center !important;
}

:deep(.label-right-align) .el-form-item__label {
  text-align: right;
  justify-content: flex-end !important;
}
</style>